<?php
//error_reporting(E_ALL);
//就近 www.jiujin.me
global $_W, $_GPC;
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($operation == 'display') {
        ca('experience.list.view');
	$pindex = max(1, intval($_GPC['page']));
	$psize = 20;
	$list = pdo_fetchall("SELECT * FROM " . tablename('ewei_shop_commission_experience') . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
        foreach($list as $k=>$v){
        //已使用的张数
        $usetotal=  pdo_fetchcolumn('select count(*) from '.tablename('ewei_shop_commission_experience_record').' where uniacid=:uniacid and experienceid=:experienceid and status=1 and isuse=1 order by id desc ',array(':uniacid'=>$_W['uniacid'],':experienceid'=>$v['id']));
        $list[$k]['usetotal']=$usetotal;//已使用的体验券个数
        $info=pdo_fetch('select nickname,avatar from '.tablename('ewei_shop_member')." where uniacid='{$_W['uniacid']}' and openid='{$v['openid']}'");
        $list[$k]['nickname']=$info['nickname'];
        $list[$k]['avatar']=$info['avatar'];
        $list[$k]['createtime']=date('Y-m-d H:i:s',$v['createtime']);
        $list[$k]['starttime']=date('Y-m-d',$v['starttime']);
        $list[$k]['endtime']=date('Y-m-d',$v['endtime']);
        }
        $total = pdo_fetchcolumn("SELECT * FROM " . tablename('ewei_shop_commission_experience') . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY id DESC");
	$pager = pagination($total, $pindex, $psize);
}elseif ($operation == 'record') {
        ca('experience.list.view');
	$id = intval($_GPC['record_id']);
        $condition="";
        if($id){
         $condition="and experienceid = '{$id}'";
        }
	$record = pdo_fetchAll("SELECT m.avatar,m.nickname,m.mobile,m.weixin,m.company,m.gender,m.province,m.city,m.area,r.createtime,r.usetime,r.experienceid,r.status,r.id,r.price,r.isuse,r.ordersn  FROM " . tablename('ewei_shop_commission_experience_record') . " r left  join " . tablename('ewei_shop_member') . " m on r.uid=m.uid and r.uniacid=m.uniacid" . " WHERE r.status=1 and r.uniacid=" . $_W['uniacid'] . " $condition order by r.id desc");
        foreach ($record as $key => $value){
                $experience=  pdo_fetch('select * from '.tablename('ewei_shop_commission_experience').' where uniacid=:uniacid and id=:id  order by id desc ',array(':uniacid'=>$_W['uniacid'],':id'=>$value['experienceid']));
		$record[$key]['title'] = $experience['title'];
                $record[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                if($value['usetime']!=0){
                $record[$key]['usetime'] = date('Y-m-d H:i:s',$value['usetime']);
                }else{
                $record[$key]['usetime']="";   
                }
	}
	if ($_GPC['export'] == 1) {
                ca('experience.list.export');
		foreach ($record as $key => $value){
			$experienceinfo = pdo_fetch("SELECT *  FROM " . tablename('ewei_shop_experience') . " WHERE id =:uid ",array(':uid'=>$value['experience']));
			$record[$key]['title'] = $experienceinfo['title'];
                        $record[$key]["saomatime"]=$value['cdate']." ".$value['ctime'];
                        if($value['gender']==1){
                        $record[$key]["gender"]="男";
                        }else{
                        $record[$key]["gender"]="女";  
                        }
                        if($value['status']==0){
                        $record[$key]["isuse"]="未使用";    
                        }else{
                        $record[$key]["isuse"]="已使用";    
                        }
                        $record[$key]["address"]=$value['province'].' '.$value['city'].''.$value['area']; 
		}
		
		$columns = array(
                    array('title' => 'uid', 'field' => 'uid', 'width' => 12),
                    array('title' => '昵称', 'field' => 'nickname', 'width' => 12), 
                    array('title' => '微信号', 'field' => 'weixin', 'width' => 12),
                    array('title' => '扫码活动主题', 'field' => 'title', 'width' => 12),
                    array('title' => '扫码活动ID', 'field' => 'experience', 'width' => 12),
                    array('title' => "扫码时间", 'field' => 'saomatime', 'width' => 24),
                    array('title' => '姓名', 'field' => 'realname', 'width' => 12),
                    array('title' => '手机号码', 'field' => 'mobile', 'width' => 12),
                    array('title' => '性别', 'field' => 'gender', 'width' => 12),
                    array('title' => '所在城市', 'field' => 'address', 'width' => 12),
                    array('title' => '是否使用', 'field' => 'isuse', 'width' => 12)
               );
                m('excel')->export($record, array("title" => "体验券导出" . date('Y-m-d-H-i', time()), "columns" => $columns));
	}
}elseif ($operation == 'paylog') {
        ca('experience.list.view');
        $condition="";
        $record = pdo_fetchAll("SELECT m.avatar,m.nickname,m.mobile,m.weixin,m.company,m.gender,m.province,m.city,m.area,r.paytime,l.tid,l.type,l.fee  FROM " . tablename('ewei_shop_commission_experience_paylog') . " l left  join " . tablename('ewei_shop_member') . " m on l.openid=m.uid and l.uniacid=m.uniacid" . " left join".tablename("ewei_shop_commission_experience_record")."as r on r.ordersn=l.tid and r.status=l.status  WHERE r.status=1 and r.uniacid=" . $_W['uniacid'] . " $condition order by r.id desc");
        foreach ($record as $key => $value){
		$record[$key]['paytime'] = date('Y-m-d H:i:s',$value['paytime']);
	}
	if ($_GPC['export'] == 1) {
                ca('experience.list.export');
		foreach ($record as $key => $value){
			$experienceinfo = pdo_fetch("SELECT *  FROM " . tablename('ewei_shop_experience') . " WHERE id =:uid ",array(':uid'=>$value['experience']));
			$record[$key]['title'] = $experienceinfo['title'];
                        $record[$key]["saomatime"]=$value['cdate']." ".$value['ctime'];
                        if($value['gender']==1){
                        $record[$key]["gender"]="男";
                        }else{
                        $record[$key]["gender"]="女";  
                        }
                        if($value['status']==0){
                        $record[$key]["isuse"]="未使用";    
                        }else{
                        $record[$key]["isuse"]="已使用";    
                        }
                        $record[$key]["address"]=$value['province'].' '.$value['city'].''.$value['area']; 
		}
		
		$columns = array(
                    array('title' => 'uid', 'field' => 'uid', 'width' => 12),
                    array('title' => '昵称', 'field' => 'nickname', 'width' => 12), 
                    array('title' => '微信号', 'field' => 'weixin', 'width' => 12),
                    array('title' => '扫码活动主题', 'field' => 'title', 'width' => 12),
                    array('title' => '扫码活动ID', 'field' => 'experience', 'width' => 12),
                    array('title' => "扫码时间", 'field' => 'saomatime', 'width' => 24),
                    array('title' => '姓名', 'field' => 'realname', 'width' => 12),
                    array('title' => '手机号码', 'field' => 'mobile', 'width' => 12),
                    array('title' => '性别', 'field' => 'gender', 'width' => 12),
                    array('title' => '所在城市', 'field' => 'address', 'width' => 12),
                    array('title' => '是否使用', 'field' => 'isuse', 'width' => 12)
               );
                m('excel')->export($record, array("title" => "体验券导出" . date('Y-m-d-H-i', time()), "columns" => $columns));
	}
}
load()->func('tpl');
include $this->template('web/experience/list');